window.onload = function (){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9003/showAllMerchant?flag=2",true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">商店名</th>\n' +
            '        <th style="border: solid;">用户名</th>\n' +
            '        <th style="border: solid;">法 人</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='pass(this)'>通过申请</button>" + "</td>" +
                "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='fail(this)'>不通过申请</button>" + "</td>" +

                "</tr>"
        }
    }
}

let show = document.getElementById("show")
let search = document.getElementById("search")
search.onclick = function () {
    let shopName = document.getElementById("shopName").value;
    let userName = document.getElementById("userName").value
    let legal = document.getElementById("legal").value
    let apply = document.getElementById("apply").value
        const xhr = new XMLHttpRequest();
    let a=""
    if (apply==="所有申请")a=4;
    if (apply==="申请中")a=2;
    if (apply==="未通过申请")a=3;
    xhr.open("get", "http://localhost:9003/showAllMerchant?flag="+a+"&shopName=" + shopName + "&userName=" + userName + "&legal=" + legal , true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">商店名</th>\n' +
            '        <th style="border: solid;">用户名</th>\n' +
            '        <th style="border: solid;">法 人</th>\n' +
            '      </tr>'
        if (a===2){
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='pass(this)'>通过申请</button>" + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='fail(this)'>不通过申请</button>" + "</td>" +
                    "</tr>"
            }
        }
        if (a===3){
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "</tr>"
            }
        }
    }


}


function fail(e){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9003/fail?user_name="+e.id, true)
    xhr.send()
    xhr.onload = function () {
        alert(e.id+"申请不通过")
        const xhr = new XMLHttpRequest();
        xhr.open("get", "http://localhost:9003/showAllMerchant?flag=2",true)
        xhr.send()
        xhr.onload = function () {
            show.innerHTML='<tr  style="border: solid;">\n' +
                '        <th style="border: solid;">商店名</th>\n' +
                '        <th style="border: solid;">用户名</th>\n' +
                '        <th style="border: solid;">法 人</th>\n' +
                '      </tr>'
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='pass(this)'>通过申请</button>" + "</td>" +

                    "</tr>"
            }
        }
    }
}


function pass(e){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9001/pass?user_name="+e.id, true)
    xhr.send()
    xhr.onload = function () {
        alert(e.id+"申请通过")
        const xhr = new XMLHttpRequest();
        xhr.open("get", "http://localhost:9003/showAllMerchant?flag=2",true)
        xhr.send()
        xhr.onload = function () {
            show.innerHTML='<tr  style="border: solid;">\n' +
                '        <th style="border: solid;">商店名</th>\n' +
                '        <th style="border: solid;">用户名</th>\n' +
                '        <th style="border: solid;">法 人</th>\n' +
                '      </tr>'
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='pass(this)'>通过申请</button>" + "</td>" +

                    "</tr>"
            }
        }
    }
}
let logoutBtn = document.getElementById("logoutBtn")
logoutBtn.onclick = function (){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8888/logout?name=&kind=admin",true)
    xhr.send()
    window.location.href="http://localhost:8888/back"
}